/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.data.common;

/**
 *
 */
public class DataColumn
{
  /** Constants */
  public static final byte TYPE_INT = 0;
  public static final byte TYPE_BOOL = 1;
  public static final byte TYPE_NUMERIC = 2;
  public static final byte TYPE_TEXT = 3;

  /** Fields */
  private int mId;
  public String name;
  public byte type;
  public boolean bCanBeNull;
  public boolean bPrimaryKey;
  public boolean bForEdit;

  /**
   * Default constructor.
   *
   * @param id the column identifier.
   * @param name the column name.
   * @param type the column type.
   * @param canBeNull whether the value in this column can be null or not.
   * @param primaryKey whether this column is the primary key or not.
   * @param forEdit whether this column is editable or not.
   */
  public DataColumn(int id, String name, byte type, boolean canBeNull,
      boolean primaryKey, boolean forEdit)
  {
    mId = id;

    this.name = name;
    this.type = type;
    this.bCanBeNull = canBeNull;
    this.bPrimaryKey = primaryKey;
    this.bForEdit = forEdit;
  }

  /**
   * Gets the column description.
   *
   * @return a complete string of the column description.
   */
  @Override
  public String toString()
  {
    String sql = name + " " + getSqlType(type);
    if (bPrimaryKey) {
      sql += " PRIMARY KEY";
    }
    if (!bCanBeNull) {
      sql += " NOT NULL";
    }
    return sql;
  }

  /**
   * Gets the string of SQL type.
   *
   * @param value the type identifier.
   * @return a string value.
   */
  public String getSqlType(int value)
  {
    switch (value)
    {
      case TYPE_INT: return "INTEGER";
      case TYPE_BOOL: return "INTEGER";
      case TYPE_NUMERIC: return "REAL";
      case TYPE_TEXT: return "TEXT";
    }
    return "TEXT";
  }

  /**
   * Gets the column identifier.
   *
   * @return an id integer.
   */
  public int getIdentifier()
  {
    return mId;
  }
}
